<?php
/** Upload
	* 
	* @package Pharmacy Intervention 
	* @subpackage Controller
	* @category Common function 
	* @author Ray Nowell 
	*/  
class Upload extends MY_Controller {
	
	function Upload()
	{
		parent::MY_Controller();
	}
	
	function index()
	{	
		$this->load->view('upload_form', array('error' => ' ' ));
	}

	function do_upload()
	{
		$this->load->helper('date');
		
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'txt|csv';
		$config['file_name'] = $this->current_user.'_'.now(); //Give the file name something unique so that there are no collisions in case multiple users upload at the same time
		//$config['max_size']	= '100';
		//$config['max_width']	= '1024';
		//$config['max_height']	= '768';
		
		$this->load->library('upload', $config);
	
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
			$this->load->view('upload_form', $error);
		}	
		else
		{
			$uploaded_data_array = $this->upload->data();
			$update_patients = $this->input->post('update_patients') == 'checked';
			$count_array = $this->_add_update_patient_records($uploaded_data_array, $update_patients);
			$this->load->view('upload_success', $count_array);
		}
	}	
	
	//This function loops through the uploaded data array and adds or updates the
	//appropriate patient record.
	//It returns an array that counts how many records were updated vs. how many were inserted.
	function _add_update_patient_records($uploaded_data_array, $update_patients)
	{
		$this->load->model('Patient_model');
		
		//$this->output->enable_profiler(TRUE);
		
		$insert_count = 0;
		$update_count = 0;
		$skipped_count = 0;
		
		//open the text file
		$fd = fopen ('./uploads/'.$uploaded_data_array['file_name'], 'r');
		// initialize a loop to go through each line of the file
		while (!feof ($fd)) 
		{
			
			$buffer = fgetcsv($fd, 4096); 
		
			for ($i = 0; $i < 3; ++$i)
			{
				switch ($i)
				{
				
					case 0:
						$patient_id_number = $buffer[$i];
						break;
					case 1:
						$first_name = $buffer[$i];
						break;
					case 2:
						$last_name = $buffer[$i];
						break;
				}
			}
			
			$patient_array = array('patient_id_number' => $patient_id_number, 
									'first_name' => $first_name, 
									'last_name' => $last_name,
									'facility_id'	=>	$this->current_facility_id,
									'modified_date'	=>	date("Y-m-d H:i:s"),
									'modified_by'	=>	$this->ion_auth->get_user()->username,
									);
			if ($this->Patient_model->insert_patient_record($patient_array))
				$insert_count++;
			else if ($update_patients && $this->Patient_model->update_patient_record($patient_array))
				$update_count++;
			else
				$skipped_count++;

		}
		fclose ($fd);
	
		//Delete the file
		unlink('./uploads/'.$uploaded_data_array['file_name']);
		
		$return_count_array = array('insert_count' => $insert_count, 'update_count' => $update_count, 'skipped_count' => $skipped_count);
		
		return $return_count_array; 
	}
}
/* End of file upload.php */
/* Location: ./system/application/controllers/upload.php */
